﻿// This code was borrowed from http://www.codeproject.com/kb/aspnet/create_template_columns.aspx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Ogdi.InteractiveSdk.WebForms
{
    public class EntityIdCustomGridViewTemplate : ITemplate
    {
        private DataControlRowType templateType;
        private string elementName;

        public EntityIdCustomGridViewTemplate(DataControlRowType type, string elName)
        {
            templateType = type;
            elementName = elName;
        }

        public void InstantiateIn(System.Web.UI.Control container)
        {
            // Create the content for the different row types.
            switch (templateType)
            {
                case DataControlRowType.Header:
                    // Create the controls to put in the header
                    // section and set their properties.
                    Label HeaderLabel = new Label();
                    HeaderLabel.Text = elementName;

                    // Add the controls to the Controls collection
                    // of the container.
                    container.Controls.Add(HeaderLabel);
                    break;
                case DataControlRowType.DataRow:
                    // Create the controls to put in a data row
                    // section and set their properties.
                    var img = new Image();

                    // To support data binding, register the event-handling methods
                    // to perform the data binding. Each control needs its own event
                    // handler.
                    img.DataBinding += new EventHandler(this.img_DataBinding);

                    // Add the controls to the Controls collection
                    // of the container.
                    container.Controls.Add(img);
                    break;

                // Insert cases to create the content for the other 
                // row types, if desired.

                default:
                    // Insert code to handle unexpected values.
                    break;
            }
        }

        private void img_DataBinding(Object sender, EventArgs e)
        {
            // Get the Label control to bind the value. The Label control
            // is contained in the object that raised the DataBinding 
            // event (the sender parameter).
            Image img = (Image)sender;

            // Get the GridViewRow object that contains the Label control. 
            GridViewRow row = (GridViewRow)img.NamingContainer;

            // Get the field value from the GridViewRow object and 
            // assign it to the Text property of the Label control.
            try
            {
                img.Height = 15;
                img.AlternateText = "Entity Id";
                img.ToolTip = XPathBinder.Eval(row.DataItem, elementName).ToString();
                img.ImageUrl = "~/images/key.png";
            }
            catch (NullReferenceException)
            {
                img.AlternateText = string.Empty;
            }
        }
    }
}
